package com.dru.drutils.location;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class DrUtilsItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>();
	Context mContext = null;
	MapView mapView = null;
	
	/**
	 * Basic constructor for an ItemizedOverlay that will show 
	 * Title/description when pressed.  Consider using 
	 * Constructor that takes Drawable,Context,MapView to handle
	 * adding/removing this overlay from the MapView
	 * @param defaultMarker
	 */
	public DrUtilsItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}
	
	/**
	 * Basic constructor for an ItemizedOverlay that will show 
	 * Title/description when pressed.  Consider using 
	 * Constructor that takes Drawable,Context,MapView to handle
	 * adding/removing this overlay from the MapView
	 * @param defaultMarker
	 */
	public DrUtilsItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		this.populate();
	}

	/**
	 * Constructor for an ItemizedOverlay that will show 
	 * Title/description when pressed.  This constructor handles
	 * adding/removing from the MapView to reduce hassle and
	 * NPE handling when items are empty
	 * @param defaultMarker
	 */
	public DrUtilsItemizedOverlay(Drawable defaultMarker, Context context, MapView mapView) {
		super(boundCenterBottom(defaultMarker));
		this.mContext = context;
		this.mapView = mapView;
		this.populate();
	}
	
	/**
	 * Adds an overlay to the list and updates where necessary,
	 * including adding the overlay to the MapView, if the MapView
	 * is set
	 * @param overlay
	 */
	public void addOverlay(OverlayItem overlay) {
		if (overlay == null)
		{
			return;
		}
		
		if (this.overlayItems.size() == 0 && this.mapView != null)
		{
			this.mapView.getOverlays().add(this);
		}
		
		overlayItems.add(overlay);

		// workaround documented here: 
		// http://developmentality.wordpress.com/2009/10/19/android-itemizedoverlay-arrayindexoutofboundsexception-nullpointerexception-workarounds/
		this.setLastFocusedIndex(-1);
		this.populate();
	}

	/**
	 * Removes an overlay to the list, and removes the overlay from 
	 * the MapView, if the MapView is set
	 * @param overlay
	 */
	public void removeOverlay(OverlayItem overlay)
	{
		if (overlay == null)
		{
			return;
		}
		
		if (this.overlayItems.size() == 1 && this.mapView != null)
		{
			this.mapView.getOverlays().remove(this);
		}
		
		this.overlayItems.remove(overlay);

		// workaround documented here: 
		// http://developmentality.wordpress.com/2009/10/19/android-itemizedoverlay-arrayindexoutofboundsexception-nullpointerexception-workarounds/
		this.setLastFocusedIndex(-1);
		this.populate();
	}
	
	/**
	 * Removes an overlay to the list, and removes the overlay from 
	 * the MapView, if the MapView is set
	 * @param overlay
	 */
	public void removeOverlay(int i)
	{
		if (i < 0 || i >= overlayItems.size())
		{
			return;
		}
		
		if (this.overlayItems.size() == 1 && this.mapView != null)
		{
			this.mapView.getOverlays().remove(this);
		}
		
		this.overlayItems.remove(i);

		// workaround documented here: 
		// http://developmentality.wordpress.com/2009/10/19/android-itemizedoverlay-arrayindexoutofboundsexception-nullpointerexception-workarounds/
		this.setLastFocusedIndex(-1);
		this.populate();
	}
	
	/**
	 * Removes all overlay items in the list
	 */
	public void clearOverlays()
	{
		this.overlayItems.clear();
		
		// workaround documented here: 
		// http://developmentality.wordpress.com/2009/10/19/android-itemizedoverlay-arrayindexoutofboundsexception-nullpointerexception-workarounds/
		this.setLastFocusedIndex(-1);
		this.populate();
	}
	
	@Override
	protected boolean onTap(int index) {
		if (mContext != null)
		{
			OverlayItem item = overlayItems.get(index);
			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			dialog.setTitle(item.getTitle());
			dialog.setMessage(item.getSnippet());
			dialog.show();
			return true;
		}
		
		return false;
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		if ( i >= 0 && i < overlayItems.size())
		{
			return overlayItems.get(i);
		}
		
		//throw new Exception("invalid value < 0 or > size() passed to DrUtilsItemizedOverlay.createItem(int)");
		return null;
	}

	@Override
	public int size() {
		return overlayItems.size();
	}

}
